package com.asset.dispoal;

import com.alibaba.fastjson2.JSON;
import com.asset.dispoal.business.bsEvidenceRecognition.api.BusinessDataManageService;
import com.asset.dispoal.business.match.dto.MatchRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.springboot.redis.commands.IRedisExtCommands;

import java.util.Map;

@Slf4j
@Component
@Order(value = 1)
public class BusinessInitStarter implements CommandLineRunner {

    @Autowired
    IRedisExtCommands redisExtCommands;

    @Autowired
    BusinessDataManageService businessDataManageService;

    @Override
    public void run(String... args) {
        //读取ocr匹配情况
        log.info("正在读取识别后的key");
        Map<String, String> matchResultMap = redisExtCommands.hgetall(6, "err_key");
        if (CollectionUtil.isNotEmpty(matchResultMap)) {
            for (String key : matchResultMap.keySet()) {
                String matchRequestStr = matchResultMap.get(key);
                MatchRequest matchRequest = JSON.parseObject(matchRequestStr, MatchRequest.class);
                businessDataManageService.evidenceMatchResult(matchRequest);
                redisExtCommands.hdel(6, "err_key", key);
            }
        }
        log.info("读取识别key处理完成，正在处理合并的key");
        Map<String, String> mergeResultMap = redisExtCommands.hgetall(6, "merge_err_key");
        if (CollectionUtil.isNotEmpty(mergeResultMap)) {
            for (String key : mergeResultMap.keySet()) {
                String mergeRequestStr = mergeResultMap.get(key);
                MatchRequest mergeRequest = JSON.parseObject(mergeRequestStr, MatchRequest.class);
                businessDataManageService.mergeCallBack(mergeRequest);
                redisExtCommands.hdel(6, "merge_err_key", key);
            }
        }
        log.info("读取合并的key完成,正在处理身份证过期的key");
        Map<String, String> idCardResultMap = redisExtCommands.hgetall(6, "idcard_err_key");
        if (CollectionUtil.isNotEmpty(idCardResultMap)) {
            for (String key : idCardResultMap.keySet()) {
                String mergeRequestStr = idCardResultMap.get(key);
                MatchRequest mergeRequest = JSON.parseObject(mergeRequestStr, MatchRequest.class);
                businessDataManageService.idCardCallBack(mergeRequest);
                redisExtCommands.hdel(6, "idcard_err_key", key);
            }
        }
        log.info("身份证过期处理完成,正在处理地址的key");
        Map<String, String> addressResultMap = redisExtCommands.hgetall(6, "address_err_key");
        if (CollectionUtil.isNotEmpty(addressResultMap)) {
            for (String key : addressResultMap.keySet()) {
                String mergeRequestStr = addressResultMap.get(key);
                MatchRequest mergeRequest = JSON.parseObject(mergeRequestStr, MatchRequest.class);
                businessDataManageService.addressCallBack(mergeRequest);
                redisExtCommands.hdel(6, "address_err_key", key);
            }
        }
        log.info("地址处理完成");
    }
}
